<?php
namespace Home\Controller;
use Think\Controller;
// +----------------------------------------------------------------------
// | Fanwe 方维p2p借贷系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class UccenterController extends  BaseController{
    //首页
    public function index(){

        $root = get_baseroot();

        //检查其他设备是否已经登录,去除微信
        if ($this->is_weixin() === true) {
            $openid = \es_session::get('wx_user_openid');
            if (empty($openid)) {

            }else{
                //微信自动登录
                //echo $openid;exit;
                wap_user_login($openid);
                $user =  $user_info = \es_session::get('user_info') ;
                if (empty($user_info)) {
                    \es_session::set('user_info', '');
                    \es_cookie::delete("user_name");
                    \es_cookie::delete("user_pwd");
                    app_redirect("http://wap.yzlicai.com/index.php?m=home&c=login&a=index");
                }
            }
        }else{
            $user_info = \es_session::get('user_info') ;
            if (empty($user_info)) {
                \es_session::set('user_info', '');
                \es_cookie::delete("user_name");
                \es_cookie::delete("user_pwd");
                app_redirect("http://wap.yzlicai.com/index.php?m=home&c=login&a=index");
            }
            $user =  $user_info;

        }

        $root['session_id'] = \es_session::id();
        $user_id  = intval($user['id']);
        $info_user = get_user_info("*","id=".$user['id']);

        if ($user_id >0){
            require_once APP_ROOT_PATH."system/libs/user.php";
            $root['user_login_status'] = 1;
            //签到数据
            $t_begin_time = to_timespan(to_date(TIME_UTC,"Y-m-d"));  //今天开始
            $t_end_time = to_timespan(to_date(TIME_UTC,"Y-m-d"))+ (24*3600 - 1);  //今天结束
            $y_begin_time = $t_begin_time - (24*3600); //昨天开始
            $y_end_time = $t_end_time - (24*3600);  //昨天结束
            $user['group_name'] = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."user_group where id = ".$user['group_id']." ");
            $root['vip_id'] = $user['vip_id'];

            if(app_conf("USER_LOGIN_MONEY") <> '0'  || app_conf("USER_LOGIN_SCORE") <> '0' || app_conf("USER_LOGIN_POINT") <> 0 || app_conf("USER_LOGIN_NMC_MONEY") <> 0){
                $root['open_sign'] = 1;
                $t_sign_data = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user_sign_log where user_id = ".$user_id." and sign_date between ".$t_begin_time." and ".$t_end_time);
                if($t_sign_data)
                {
                    $root['t_sign_data'] = $t_sign_data;
                }
            }
            else{
                $root['open_sign'] = 0;
            }

            if($user['vip_id'] == 0){
                $user['vip_grade'] = "您还不是VIP会员";
            }else{
                $user['vip_grade'] = $GLOBALS['db']->getOne("select vip_grade from ".DB_PREFIX."vip_type where id = ".$user['vip_id']." ");
            }

            $credit_info =  $GLOBALS['db']->getRow("SELECT * FROM ".DB_PREFIX."user_credit_file where type='credit_identificationscanning' and user_id =".$user_id);
            if($credit_info){
                if($credit_info['status'] == 0){
                    $root['credit_status'] = 2;
                    $root['credit_show'] = "待审核";
                }elseif($credit_info['status'] == 1){
                    if($credit_info['passed'] == 1){
                        $root['credit_status'] = 1;
                        $root['credit_show'] = "已认证";
                    }elseif($credit_info['passed'] == 2){
                        $root['credit_status'] = 3;
                        $root['credit_show'] = "审核失败";
                    }
                }
            }else{
                $root['credit_status'] = 0;
                $root['credit_show'] = "未认证";
            }


            $province_str = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."region_conf where id = ".$user['province_id']);
            $city_str = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."region_conf where id = ".$user['city_id']);
            if($province_str.$city_str=='')
                $user_location = $GLOBALS['lang']['LOCATION_NULL'];
            else
                $user_location = $province_str." ".$city_str;

            $user['user_location'] = $user_location;
            $user['money_format'] = format_price($user['money']);//可用资金
            $user['lock_money_format'] = format_price($user['lock_money']);//冻结资金
            $user['total_money'] = $user['money'] + $user['lock_money'];//资金余额
            $user['total_money_format'] = format_price($user['total_money']);//资金余额
            $user['create_time_format'] = to_date($user['create_time'],'Y-m-d'); //注册时间

            $root['user_id'] = $root['id'] = $user_id;
            $root['response_code'] = 1;
            $root['vip_grade'] = $user['vip_grade'];
            $root['user_location'] = $user['user_location'];
            $root['user_name'] = $user['user_name'];
            $root['group_name'] = $user['group_name'];
            $root['money_format'] = $user['money_format'];
            $root['money'] = $user['money'];
            $root['lock_money_format'] = $user['lock_money_format'];
            $root['lock_money'] = $user['lock_money'];
            $root['total_money'] = $user['total_money'];
            $root['total_money_format'] = $user['total_money_format'];
            $root['create_time_format'] = $user['create_time_format'];
            $root['score'] = $user['score'];
            $root['idno'] = $user['idno'];
            $root['real_name'] = $user['real_name'];
            $root['point'] = $user['point'];
            $root['quota'] = $user['quota'];
            //我的红包
            $root['voucher_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM ".DB_PREFIX."ecv WHERE user_id=".$user_id." AND if(end_time > 0, (end_time+24*3600-1) > ".TIME_UTC.",1=1) AND if(use_limit > 0,(use_limit - use_count) > 0,1=1)");

            //我的加息券
            $root['interestrate_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM ".DB_PREFIX."interestrate i left join  ".DB_PREFIX."interestrate_type it on i.ecv_type_id = it.id WHERE ((i.user_id=".$user_id." and i.to_user_id = 0) or i.to_user_id = ".$user_id.") AND if(i.end_time > 0, (i.end_time+24*3600-1) > ".TIME_UTC.",1=1) AND if(i.use_limit > 0,(i.use_limit - i.use_count) > 0,1=1) and it.use_type = 1 or it.use_type = 2");


            if(intval(app_conf("OPEN_IPS")) > 0){
                $app_url = SITE_DOMAIN."/index.php?m=Home&c=collocation&a=CreateNewAcct&user_type=0&user_id=".$user_id."&from=wap";
                //申请
                $root['app_url'] = $app_url;
                $root['acct_url'] = $root['app_url'];
            }

            $root['ips_acct_no'] = $user['ips_acct_no'];
            $root['open_ips'] = intval(app_conf("OPEN_IPS"));

            //第三方托管标
            if (!empty($user['ips_acct_no']) && intval(app_conf("OPEN_IPS")) > 0){
                $result = GetIpsUserMoney($user_id,0);

                $root['money'] = $user['money'] = $root['ips_balance'] = $result['pBalance'];//可用余额
                $root['lock_money'] = $user['lock_money'] = $root['ips_lock'] = $result['pLock'];//冻结余额
                $root['ips_needstl'] = $result['pNeedstl'];//未结算余额

                $user['total_money'] = $user['money'] ;//资金余额 + $user['lock_money']

                $user['money_format'] = format_price($user['money']);//可用资金

                $root['lock_money_format'] =  $user['lock_money_format'] = format_price($user['lock_money']);//冻结资金
                $root['total_money_format'] = $user['total_money_format'] = format_price($user['total_money']);//资金余额

            }else{
                $root['ips_balance'] = 0;//可用余额
                $root['ips_lock'] = 0;//冻结余额
                $root['ips_needstl'] = 0;//未结算余额

                $root['lock_money_format'] =  $user['lock_money_format'] = format_price($user['lock_money']);//冻结资金
                $root['total_money_format'] = $user['total_money_format'] = format_price($user['total_money']);//资金余额


            }

            $root['ips_balance_format'] = format_price($root['ips_balance']);
            $root['ips_lock_format'] = format_price($root['ips_lock']);
            $root['ips_needstl_format'] = format_price($root['ips_needstl']);
            $root['program_title'] = "会员中心";
            if($user_id){
                $from_who = \es_session::get('from_who');

                if($from_who && $from_who != $user_id){
                    $rs_bind = M('user')->where('id='.$user_id)->find();
                    if(empty($rs_bind['pid'])){
                            $updateData['pid'] = $from_who ;
                            M('user')->where('id='.$user_id)->save($updateData);

                    }
                }
            }
        }else{
            $root['response_code'] = 0;
            $root['show_err'] ="未登录";
            $root['user_login_status'] = 0;
            $root['program_title'] = "登陆";
        }
        $root['act'] = CONTROLLER_NAME;
        $root['is_ajax'] = empty($_GET['is_ajax']) ? 0 : 1 ;
        $root['is_login'] = $this->check_is_login() ;
        $this->assign('data',$root);
        $this->display();
    }
    public function is_weixin() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }
    public function login()
    {
        $email = strim($_REQUEST['email']);//用户名或邮箱
        $pwd = strim($_REQUEST['pwd']);//密码
        $root = get_baseroot();
        require_once APP_ROOT_PATH."system/libs/user.php";
        $result = do_login_user($email,$pwd);
        if($result['status'])
        {
            $user_data = \es_session::get('user_info');

            //登录成功，更新单设备登录令牌
            $token = md5(uniqid(microtime(true).$user_data['id'],true));
            \es_cookie::set("token",$token,3600*24*30);
            \es_cookie::set("user_name",$user_data['user_name'],3600*24*30);
            $rs = $GLOBALS['db']->autoExecute(DB_PREFIX."user",array("token"=>$token),"UPDATE","id=".$user_data['id']);

            $root['response_code'] = 1;
            $root['user_login_status'] = 1;//用户登陆状态：1:成功登陆;0：未成功登陆
            $root['show_err'] = "用户登陆成功";
            $root['id'] = $user_data['id'];
            $root['user_name'] = $user_data['user_name'];
            $root['user_pwd'] = $user_data['user_pwd'];
            $root['user_money'] = $user_data['money'];
            $root['user_money_format'] = format_price($user_data['money']);//用户金额

            /*
            $root['home_user']['fans'] = $user_data['focused_count'];
            $root['home_user']['photos'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."topic_image where user_id = ".$user_data['id']);
            $root['home_user']['goods'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."topic where user_id = ".$user_data['id']." and topic_group = 'Fanwe' and is_delete = 0 and is_effect = 1");
            $root['home_user']['follows'] = $user_data['focus_count'];
            $root['home_user']['favs'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."topic where user_id = ".$user_data['id']." and fav_id <> 0");

            $root['home_user']['user_avatar'] = get_abs_img_root(get_muser_avatar($user_data['id'],"big"));
            $root['user_avatar'] = get_abs_img_root(get_muser_avatar($user_data['id'],"big"));

            if(strim($_REQUEST['sina_id'])!='')
            {
                if($GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."user where sina_id = '".strim($_REQUEST['sina_id'])."'")==0)
                {
                    $access_token =  trim($_REQUEST['access_token']);
                    $GLOBALS['db']->query("update ".DB_PREFIX."user set sina_id = '".strim($_REQUEST['sina_id'])."',sina_token = '".$access_token."' where id = ".$user_data['id']);

                }


            }
            if(strim($_REQUEST['tencent_id'])!='')
            {
                if($GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."user where tencent_id = '".strim($_REQUEST['tencent_id'])."'")==0)
                {
                $GLOBALS['db']->query("update ".DB_PREFIX."user set tencent_id = '".strim($_REQUEST['tencent_id'])."' where id = ".$user_data['id']);

                $openid = trim($_REQUEST['openid']);
                $openkey = trim($_REQUEST['openkey']);
                 $access_token =  trim($_REQUEST['access_token']);
                $GLOBALS['db']->query("update ".DB_PREFIX."user set t_access_token ='".$access_token."',t_openkey = '".$openkey."',t_openid = '".$openid."', login_ip = '".CLIENT_IP."',login_time= ".get_gmtime()." where id =".$user_data['id']);
                }

            }
            */

        }
        else
        {
            $root['response_code'] = 0;
            $root['user_login_status'] = 0;//用户登陆状态：1:成功登陆;0：未成功登陆
            if($result['data'] == ACCOUNT_NO_EXIST_ERROR)
            {
                $err = $GLOBALS['lang']['USER_NOT_EXIST'];
            }
            if($result['data'] == ACCOUNT_PASSWORD_ERROR)
            {
                $err = $GLOBALS['lang']['PASSWORD_ERROR'];
            }
            if($result['data'] == ACCOUNT_NO_VERIFY_ERROR)
            {
                $err = $GLOBALS['lang']['USER_NOT_VERIFY'];
            }

            $root['show_err'] = $err;
            $root['id'] = 0;
            $root['user_name'] = $email;
            $root['user_email'] = $email;
        }

        $root['program_title'] = "登录";

        echo json_encode($root);
    }
}
?>